const suffixMap: Record<string, string> = {
  // image
  png: 'image',
  jpg: 'image',
  jpeg: 'image',
  svg: 'image',

  // text
  txt: 'text',
  html: 'text',
  md: 'text',
  xml: 'text',
  yaml: 'text',
  yml: 'text',
  json: 'text',

  // pdf
  pdf: 'pdf',

  // office
  doc: 'office',
  docx: 'office',
  xls: 'office',
  xlsx: 'office',
  ppt: 'office',
  pptx: 'office',

  // audio
  mp3: 'audio',
  ogg: 'audio',
  wma: 'audio',
  aac: 'audio',
  wav: 'audio',

  // cad
  dwg: 'cad',
  dxf: 'cad',

  // video
  mp4: 'video',
  webm: 'video',
  avi: 'video'
}

export const previewTypes = ['text', 'image', 'pdf', 'office', 'video', 'audio', 'cad']

export const editTypes = ['office']

export const printTypes = ['text', 'image', 'pdf', 'office']

function resolveSuffix (name = '') {
  return name.slice(name.lastIndexOf('.') + 1).toLowerCase()
}

export function getType (name: string) {
  if (!name) return ''
  const type = suffixMap[resolveSuffix(name)] as any
  return type || ''
}

export function canPreview (name: string) {
  return previewTypes.includes(getType(name))
}

export function canEdit (name: string) {
  return editTypes.includes(getType(name))
}

export function canPrint (name: string) {
  return printTypes.includes(getType(name))
}

export function getSuffixesByType (type: string): string[] {
  return Object.entries(suffixMap).filter(([k, v]) => v === type).map(([k, v]) => k)
}
